package org.travel.app.controller.manage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.travel.app.entity.spots.SpotsImag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author nmm
 * @date 2025-01-2025/1/7
 * @deriction njm......
 */

@RestController
@RequestMapping("/SpotsImagUpload")
public class SpotsImagUploadController {

    @Autowired
    private  RedisTemplate<String,Object> redisTemplate;

    /**
     * 使用 ConcurrentHashMap 作为临时存储
     */
    private final Map<String, List<String>> scenicImages = new ConcurrentHashMap<>();

    @PostMapping("/{spotsId}/upload")
    public String uploadImages(@PathVariable String spotsId, @RequestParam("files") MultipartFile[] files) throws IOException {
        List<String> imageList = scenicImages.getOrDefault(spotsId, new ArrayList<>());

        for (MultipartFile file : files) {
            byte[] bytes = file.getBytes();
            String base64Image = Base64.getEncoder().encodeToString(bytes);
        }

        scenicImages.put(spotsId, imageList);
        redisTemplate.opsForValue().set(spotsId, imageList);
        return "图片上传成功";
    }

    @GetMapping("/{scenicId}/images")
    public SpotsImag getImages(@PathVariable String scenicId) {
        Object  images = redisTemplate.opsForValue().get(scenicId);
        if (images instanceof List<?>) {
            return (SpotsImag) images;
            // 安全地进行类型转换
        } else {
            // 处理未找到或类型不匹配的情况
            return null;
            // 或者返回一个空列表
        }
    }
}
